<?php
/**
 * Created by PhpStorm.
 * User: antely
 * Date: 2018/4/26
 * Time: 11:58
 * 用户信息验证类
 */
namespace app\api\validate;
use think\{Validate,Db};
class Live extends Validate{
    protected $rule = [
        ['id',                   'require|number|checkExist',   '-50000|-50001'],
        ['web_id',                   'require',   '-10789'],
        ['uid',                   'number',                  '-50002'],
        ['title',                   'regex:/^[\x{4e00}-\x{9fa5}a-zA-Z0-9\_\.-。(){}\""\'\:;?【】<>#￥%$&*《》]+$/u|length:1,20|checkTitleExist',               '	-50030|-50029'],
        ['start_time',                   'number',        '-50005'],
        ['end_time',                   'number',        '-50006'],
        ['is_change_price',                   'boolean',        '-50009'],
        ['current_room', 'alphaDash' ,       '-50021'],
        ['status',                   'in:0,1,2,-1|number',        '-50035|-50010'],
        ['anchor_id',                   'number|checkAnchorExist',        '-50013'],
        ['price_range',                   'number|checkPriceRange',        '-50025']
    ];
    protected $scene = [
        'add'=>['uid','title','web_id','anchor_id','start_time','end_time','is_change_price','current_room','current_product','price_range'],
        'update'=>['id','uid','web_id','title','anchor_id','start_time','end_time','is_change_price','current_room','current_product','status','price_range'],
        'delete_live'=>['id']
    ];

    public function checkExist($value,$data){
        return Db::connect(config('database.db_config1'))->name('Live')->where(['id'=>$value])->where('status','neq',-1)->count('id')==1?true:-50003;
    }

    public function checkTitleExist($value,$rule,$data){

        if(!empty($data['id'])){
            return Db::connect(config('database.db_config1'))->name('Live')->where(['title'=>$data['title']])->where('id != '.$data['id'].'')->where('status','neq',-1)->count()==0?true:-50004;
        }else{
            return Db::connect(config('database.db_config1'))->name('Live')->where(['title'=>$value])->where('status','neq',-1)->count('id')==1?-50004:true;
        }
    }


    public function checkAnchorExist($value){
        return Db::connect(config('database.db_config1'))->name('Anchor')->where(['id'=>$value])->where('status','neq',-1)->count('id')==1?true:-50012;
    }

    public function checkPriceRange($value,$data){
        if($value<0||$value>20){
            return -50026;
        }else{
            return true;
        }
    }



}